// @ts-nocheck
// 专门做兼容处理字段函数

// type
import { TriggerFlowData } from '@src/modules/trigger/model/interface/trigger'
import { CompatibilityField }  from '@src/modules/trigger/types/compatibility.ts'

/* utils */
import { convertTriggerAppFormFieldValue } from '@src/modules/connector/util/card.ts'
import _ from 'lodash'

/**
 * @description 兼容字段处理函数
 * @param data 前端转后端数据
 * @param _CField 兼容的字段信息
 * @returns TriggerFlowData 
 */
export function compatibility(data: TriggerFlowData | null, _CField: CompatibilityField[]): TriggerFlowData | null {
    if (!data) return null
    try {
        _CField.forEach(item => {
            // 这是多选字段 兼容后端
            if (item._isMultiCascader) {
                let node = data.nodeList.find(i => i.uniqueId === item.nodeUniqueId)
                let json = JSON.parse(node.options.fieldOptions.insert[item.index].value)
                let params = json.map(j => {
                    return j.map(k => {
                        return k
                    }).join('/')
                })
                node.options.fieldOptions.insert[item.index].value = [JSON.stringify(params)]
            }

            // 这是来自其他表单，兼容后端
            if (item._isOtherFormCondition) {
                let node = data.lineList.find(i => (i?.uniqueId || i.id) === item.nodeUniqueId)
                let _cv = node.options.conditionList[item.i][item.j].value[0]

                _cv = convertTriggerAppFormFieldValue(_cv)
                _cv = JSON.parse(_cv)
                Reflect.has(_cv, 'toRequestFieldList') && Reflect.deleteProperty(_cv, 'toRequestFieldList')
                Reflect.has(_cv, 'toResponseFieldList') && Reflect.deleteProperty(_cv, 'toResponseFieldList')

                node.options.conditionList[item.i][item.j].value[0] = JSON.stringify(_cv)
            }

            // 这是来自其他表单的执行节点，兼容后端
            if (item._isOtherFormExecute) {
                let node = data.nodeList.find(i => i.uniqueId === item.nodeUniqueId)
                let _cv
                // 这里有三种状态 分别取不同的值 create update delete
                if (item.status == 'create') {
                    _cv = node.options.fieldOptions.insert[item.index].value[0]
                } else if (item.status == 'update') {
                    _cv = node.options.fieldOptions.update[item.index].value[0]
                } else if (item.status == 'select') {
                    _cv = node.options.fieldOptions.insert[item.index].value[0]
                }
                _cv = convertTriggerAppFormFieldValue(_cv)
                _cv = JSON.parse(_cv)
                Reflect.has(_cv, 'toRequestFieldList') && Reflect.deleteProperty(_cv, 'toRequestFieldList')
                Reflect.has(_cv, 'toResponseFieldList') && Reflect.deleteProperty(_cv, 'toResponseFieldList')

                if (item.status == 'create') {
                    node.options.fieldOptions.insert[item.index].value[0] = JSON.stringify(_cv)
                } else if (item.status == 'update') {
                    node.options.fieldOptions.update[item.index].value[0] = JSON.stringify(_cv)
                } else if (item.status == 'select') {
                    node.options.fieldOptions.insert[item.index].value[0] = JSON.stringify(_cv)
                }
            }

            // 指定节点的执行动作的条件，其他表单，兼容后端
            if (item._isExecuteActionUpdateCondition) {
                let node = data.nodeList.find(i => i.uniqueId === item.nodeUniqueId)
                // 该模式简单粗暴 性能不是最优解
                // node?.options.fieldOptions.select.forEach((ii, index) =>{
                //     if (ii.valueType == '3') {
                //         let _cv = ii.value[0]
                //         _cv = convertTriggerAppFormFieldValue(_cv)
                //         _cv = JSON.parse(_cv)
                //         Reflect.has(_cv, 'toRequestFieldList') && Reflect.deleteProperty(_cv, 'toRequestFieldList')
                //         Reflect.has(_cv, 'toResponseFieldList') && Reflect.deleteProperty(_cv, 'toResponseFieldList')

                //         node.options.fieldOptions.select[index].value[0] = JSON.stringify(_cv)
                //     }
                // })

                let _cv = node.options.fieldOptions.select[item.index].value[0]
                _cv = convertTriggerAppFormFieldValue(_cv)
                _cv = JSON.parse(_cv)
                Reflect.has(_cv, 'toRequestFieldList') && Reflect.deleteProperty(_cv, 'toRequestFieldList')
                Reflect.has(_cv, 'toResponseFieldList') && Reflect.deleteProperty(_cv, 'toResponseFieldList')
                node.options.fieldOptions.select[item.index].value[0] = JSON.stringify(_cv)
            }

            // 指定节点的执行动作的删除 条件 其他表单 兼容后端数据
            if (item._isExecuteActionDeleteCondition) {
                let node = data.nodeList.find(i => i.uniqueId === item.nodeUniqueId)
                // 该模式简单粗暴 性能不是最优解
                // node?.options.fieldOptions.delete.forEach((ii, index) =>{
                //     if (ii.valueType == '3') {
                //         let _cv = ii.value[0]
                //         _cv = convertTriggerAppFormFieldValue(_cv)
                //         _cv = JSON.parse(_cv)
                //         Reflect.has(_cv, 'toRequestFieldList') && Reflect.deleteProperty(_cv, 'toRequestFieldList')
                //         Reflect.has(_cv, 'toResponseFieldList') && Reflect.deleteProperty(_cv, 'toResponseFieldList')

                //         node.options.fieldOptions.delete[index].value[0] = JSON.stringify(_cv)
                //     }
                // })
                let _cv = node.options.fieldOptions.delete[item.index].value[0]
                _cv = convertTriggerAppFormFieldValue(_cv)
                _cv = JSON.parse(_cv)
                Reflect.has(_cv, 'toRequestFieldList') && Reflect.deleteProperty(_cv, 'toRequestFieldList')
                Reflect.has(_cv, 'toResponseFieldList') && Reflect.deleteProperty(_cv, 'toResponseFieldList')
                node.options.fieldOptions.delete[item.index].value[0] = JSON.stringify(_cv)
            }

            // 如果是定时任务触发 需要将格式化数据转为不是格式化数据
            if (item._isTimerJsonParse) {
                try {
                    _.set(data, item.execute, JSON.stringify(JSON.parse(_.get(data, item.execute))))
                } catch (error) {
                    console.error(error)
                }
            }
        })
    } catch(err) {
        console.error(err)
    }

    return data
}